import axios from 'axios'
import Vue from 'vue';
import { Toast } from 'vant';
import store from '@/store'
Vue.use(Toast);

if(process.env.NODE_ENV=='development'){
    axios.defaults.baseURL = process.env.VUE_APP_HOST;
}else{
    axios.defaults.baseURL = process.env.VUE_APP_API_HOST;
}

axios.interceptors.request.use(
    (config) => {
        config.headers['from'] = 'wap';
        // config.headers['ContentType']='application/json';
        // do something before request is sent
        if (store.getters.token) {
            //config.headers['Authorization'] = 'Bearer ' + store.getters.token
            config.headers['token'] = store.getters.token;
        }
        return config
    },
    (error) => {
        // do something with request error
        return Promise.reject(error)
    }
)

// //异步请求返回统一处理
axios.interceptors.response.use(async (response) => {
        if(response.config.url=='/api/exportFile'){
            return response.data
        }
        if (!response.data.status) { // 服务端返回的状态码不等于200，则reject()
            Toast(response.data.msg);
            console.log(msg);
        }
        return response.data.data;
    },
    (error) => {
        console.log(error);
        return Promise.reject(error);
    }
);
export {
    axios
}
